/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.ChiTietTiecCuoi;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class ChiTietTiecCuoiDAO {
     
    /**
     * @desc lay danh sach chi tiet tiec cuoi
     * @return 
     */
    public static List<ChiTietTiecCuoi> layDanhSach() {
        List<ChiTietTiecCuoi> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select cttc from ChiTietTiecCuoi cttc";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim chi tiet tiec cuoi theo ma chi tiet tiec cuoi
     * @param maChiTietTiecCuoi
     * @return 
     */
    public static ChiTietTiecCuoi layThongTinChiTietTiecCuoi(int maChiTietTiecCuoi) {
        ChiTietTiecCuoi cttc = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            cttc = (ChiTietTiecCuoi) session.get(ChiTietTiecCuoi.class, maChiTietTiecCuoi);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return cttc;
    }
    
    /**
     * @desc them chi tiet tiec cuoi
     * @param cttc
     * @return 
     */
    public static boolean themChiTietTiecCuoi(ChiTietTiecCuoi cttc) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (ChiTietTiecCuoiDAO.layThongTinChiTietTiecCuoi(cttc.getMaChiTiet())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(cttc); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin chi tiet tiec cuoi
     * @param cttc
     * @return 
     */
    public static boolean capNhatThongTinChiTietTiecCuoi(ChiTietTiecCuoi cttc) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (ChiTietTiecCuoiDAO.layThongTinChiTietTiecCuoi(cttc.getMaChiTiet()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(cttc); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa chi tiet tiec cuoi
     * @param maChiTietTiecCuoi
     * @return 
     */
    public static boolean xoaChiTietTiecCuoi(int maChiTietTiecCuoi) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        ChiTietTiecCuoi cttc = ChiTietTiecCuoiDAO.layThongTinChiTietTiecCuoi(maChiTietTiecCuoi); 
        if(cttc==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(cttc); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
